package com.sunjob.mroimg.service;

import com.sunjob.common.domain.img.SysImg;
import com.sunjob.mroimg.config.ImgConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Wangzurong
 * @create 2022-08-09 9:45
 * 图片文件服务实现类
 */
@Service
public class ImgFileServiceImpl implements ImgFileService {
    @Autowired
    ImgConfiguration imgConfiguration;
    @Override
    public Boolean saveToLocal(SysImg sysImg) {
        // 获取字节数组
        byte[] data = sysImg.getData();
        //  获取映射地址
        String url = sysImg.getImgUrl();
        // 获取本地字节
        FileOutputStream ou = null;
        try {
             ou = new FileOutputStream(imgConfiguration.getLocation()+url);
             ou.write(data,0,data.length);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if(ou!=null){
                try {
                    ou.close();
                }catch (Exception e){
                    return false;
                }
            }
        }
        return true;
    }
}
